//
//  FuzzySearchView.m
//  QFBAPP
//
//  Created by 无常先生 on 2018/5/28.
//  Copyright © 2018年 YIXun. All rights reserved.
//

#import "FuzzySearchView.h"
#import <ReactiveObjC/ReactiveObjC.h>

@interface FuzzySearchView () <UITextFieldDelegate>
@property (nonatomic, copy) NSString *placeholder;
@property (nonatomic, copy) void (^textChangeBlock)(NSString *);
@property (nonatomic, copy) void (^cancelAction)(void);
@property (nonatomic, copy) void (^returnClickCallBack)(NSString *searchText);

@property (nonatomic, strong) UITextField *textField;

@property (nonatomic, strong) UIButton *cancelButton;
// 点击输入框时，拦截输入框的输入响应
@property (nonatomic, strong) UIButton *clickBtn;
@end
static CGFloat const actionW = 50;
@implementation FuzzySearchView

- (instancetype)initWithFrame:(CGRect)frame placeholderText:(NSString *)placeholder inputTextDidChange:(void (^)(NSString *))textChangeBlock cancelClick:(void (^)(void))cancelClick {
    
    if (self = [super initWithFrame:frame]) {
        self.placeholder = placeholder;
        self.textChangeBlock = textChangeBlock;
        self.cancelAction = cancelClick;
        
        [self prepareUI];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.layer.cornerRadius = 4;
    [self setClipsToBounds:YES];
    if(self.hiddenCancel) {
        self.textField.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    }else {
        self.cancelButton.frame = CGRectMake(self.frame.size.width - (actionW - 3), 0, actionW, self.frame.size.height);
        
        self.textField.frame = CGRectMake(0, 0, self.frame.size.width - actionW, self.frame.size.height);
    }
    self.clickBtn.frame = self.textField.bounds;
    
}


- (void)prepareUI {
    
    self.textField = [[UITextField alloc]init];
    self.textField.backgroundColor = [UIColor whiteColor];
    self.textField.placeholder = self.placeholder;
    self.textField.textColor = [UIColor blackColor];
    self.textField.font = [UIFont systemFontOfSize:14];
    self.textField.tintColor = [UIColor orangeColor];
    self.textField.layer.cornerRadius = 4;
    UIImageView *iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_search_grey"]];
    iconView.contentMode = UIViewContentModeScaleAspectFit;
    iconView.frame = CGRectMake(0, 0, 50,30);
    self.textField.leftView = iconView;
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    self.textField.clearButtonMode = UITextFieldViewModeAlways;
    self.textField.delegate = self;
    self.textField.returnKeyType = UIReturnKeySearch;
//    [self.textField addTarget:self action:@selector(inputTextDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:self.textField];
    
    
    self.clickBtn = [[UIButton alloc]init];
    [self.clickBtn addTarget:self action:@selector(selectButtonClick) forControlEvents:UIControlEventTouchUpInside];
    self.clickBtn.hidden = YES;
    [self.textField addSubview:self.clickBtn];
    
    
    self.cancelButton = [[UIButton alloc]init];
    self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.cancelButton setTitle:@"搜索" forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.cancelButton];
    
     @weakify(self);
    [[[self.textField.rac_textSignal throttle:0.35]distinctUntilChanged]subscribeNext:^(NSString * _Nullable x) {
        @strongify(self);
        if (self.textChangeBlock) {
            self.textChangeBlock(x);
        }
    }];
}

#pragma mark - setter
-(void)setOldSearchKey:(NSString *)oldSearchKey {
    _oldSearchKey = oldSearchKey;
    self.textField.text = oldSearchKey;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:backgroundColor];
    self.textField.backgroundColor = backgroundColor;
}

- (void)selectButtonClick{
    if (self.searchViewDidEditClick) {
        self.searchViewDidEditClick();
    }
}
- (void)setSearchViewDidEditClick:(void (^)(void))searchViewDidEditClick {
    _searchViewDidEditClick = searchViewDidEditClick;
    self.clickBtn.hidden = NO;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    if (self.returnClickCallBack) {
        self.returnClickCallBack(textField.text);
    }
    return YES;
}

- (void)returnKeyboardClick:(void (^)(NSString *))callBack {
    if (callBack) {
        self.returnClickCallBack = callBack;
    }
}

- (void)cancelButtonClick {
    if (self.cancelAction) {
        self.cancelAction();
    }
}
- (void)becomeFirstResponder {
    [self.textField becomeFirstResponder];
}

- (void)resignFirstResponder {
    [self.textField resignFirstResponder];
}

@end
